package com.daxue.localclient.controller;

import com.daxue.grpc.lib.UserResponse;
import com.daxue.localclient.grpc.GrpcClientService;
import com.daxue.localclient.service.RetryTest;
import com.daxue.localclient.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author daxue0929
 * @date 2022/08/31
 **/
@RestController
public class GrpcTestController {

    private final GrpcClientService grpcClientService;

    public final RetryTest retryTest;

    @Autowired
    public GrpcTestController(GrpcClientService grpcClientService, RetryTest retryTest) {
        this.grpcClientService = grpcClientService;
        this.retryTest = retryTest;
    }

    @RequestMapping("/")
    public String printMessage(@RequestParam(defaultValue = "will") String name) {
        return grpcClientService.sendMessage(name);
    }

    @GetMapping("/sendUser")
    public Result sendUser() {
        UserResponse userResponse = grpcClientService.sendUser();
        return Result.success(userResponse.getMessage());
    }


    @GetMapping("/retry/test/{id}")
    public Object test(@PathVariable int id) {
        try {
            Integer integer = retryTest.retryThree(id);
            return integer;
        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
        return null;
    }
}
